<template>
  <basic-container>
    <div class="avue-crud">
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="窗口名" prop="windowName">
          <el-input v-model="queryParams.windowName" placeholder="请输入窗口名" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="窗口号" prop="windowName">
          <el-input v-model="queryParams.windowNo" placeholder="请输入窗口号" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-if="permissions.business_window_add">新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list" style="width: 100%">
        <el-table-column label="编号" align="center" prop="windowId"/>
        <el-table-column label="窗口名称" align="center" prop="windowName"/>
        <el-table-column label="窗口标识" align="center" prop="windowKey"/>
        <el-table-column label="窗口号" align="center" prop="windowNo"/>
        <el-table-column label="窗口简介" align="center" prop="windowDescribe"/>
        <el-table-column label="窗口状态" align="center" prop="windowStatus">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.WINDOW_STATUS" :value="scope.row.windowStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime">
          <template slot-scope="scope">
            <span>{{ scope.row.updateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-if="permissions.business_window_edit">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-if="permissions.business_window_del">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 对话框(添加 / 修改) -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="窗口名称" prop="windowName">
            <el-input v-model="form.windowName" placeholder="请输入窗口名称"/>
          </el-form-item>
          <el-form-item label="窗口号" prop="windowNo">
            <el-input v-model="form.windowNo" placeholder="请输入窗口号"/>
          </el-form-item>
          <el-form-item label="窗口编码" prop="windowNo">
            <el-input v-model="form.windowKey" placeholder="请输入窗口编码"/>
          </el-form-item>
          <el-form-item label="窗口简介" prop="windowDescribe">
            <el-input type="textarea" autosize v-model="form.windowDescribe" placeholder="请输入窗口简介"/>
          </el-form-item>
          <el-form-item label="排序" prop="sortOrder">
            <el-input-number
              v-model="form.sortOrder"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                  @pagination="getList"/>
    </div>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {fetchList, addObj, delObj, getObj, putObj} from "@/api/queue/window";

export default {
  name: "index",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 结果集合
      list: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        windowName: null,
        windowNo: null
      },
      // 表单校验
      rules: {
        windowName: [{required: true, message: "窗口名称不能为空", trigger: "blur"}],
        windowNo: [{required: true, message: "窗口号不能为空", trigger: "blur"}],
        windowKey: [{required: true, message: "窗口编码不能为空", trigger: "blur"}],
      }
    }
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  methods: {
    // 查询窗口列表
    getList() {
      this.loading = true
      // 处理查询参数
      let params = {...this.queryParams};
      fetchList(params).then(res => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.windowId != null) {
          putObj(this.form).then(response => {
            this.$message.success("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        addObj(this.form).then(response => {
          this.$message.success("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加窗口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.windowId;
      getObj(id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改窗口";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.windowId;
      this.$confirm('是否确认删除："' + row.windowName + '"的数据项?').then(function () {
        return delObj(id);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        windowName: undefined,
        windowDescribe: undefined,
      };
      this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
